/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fitcoders.inscripcion.controller;

//// cambios realizados en este programa

import com.fitcoders.inscripcion.model.Curso;
import com.fitcoders.inscripcion.service.CursoService;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Max
 */
@Controller
public class CursoController {

    @Autowired
    CursoService cursoService;
    

    @RequestMapping(value = "/cursos", method = RequestMethod.GET)
    public String listPersons(Model model) throws Exception {
        model.addAttribute("curso", new Curso());
        model.addAttribute("listaCursos", cursoService.list("from Curso c", null, null));
        return "curso";
    }

    @RequestMapping(value = "/process-curso")
    public String processPerson(@ModelAttribute("curso") Curso curso) throws Exception {

        if (curso.getId() == null) {
            //new person, add it
            cursoService.save(curso);
        } else {
            //existing person, call update
            cursoService.update(curso);
        }
        
        return "redirect:/cursos.htm";
    }
    
    @RequestMapping("/edit/{id}")
    public String editPerson(@PathVariable("id") int id, Model model) throws Exception {
        Curso c = cursoService.getByHQL("from Curso c where c.id="+id, null);
        model.addAttribute("curso",  c);
        model.addAttribute("listaCursos", cursoService.list("from Curso c", null, null));
        
        return "curso";
    }
}
